<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * 微信用户模型
 * @package App\Models
 */
class WxUser extends Model
{
    use SoftDeletes;
    //主键
    protected $primaryKey = 'user_id';
    protected $dateFormat = 'Y-m-d H:i:s';
    protected $dates = [
        'first_subscribe_time',
        'subscribe_time',
    ];
    /**
     * 应该被转换成原生类型的属性。
     * @var array
     */
    protected $casts = [
        'tagid_list' => 'array',
    ];

    /**
     * 用户关注的渠道来源访问器
     * @return bool|string
     */
    public function getSubscribeSceneNameAttribute()
    {
        return (new \App\Services\WxUserService())->getSubscribeSceneName($this->subscribe_scene);
    }

    /**
     * 性别名称访问器
     * @return mixed|string
     */
    public function getSexNameAttribute()
    {
        $sexConf = [0 => '未知', 1=>'男性',2 => '女性',];
        return $sexConf[$this->sex] ?? '';
    }

    /**
     * 用户标签名称列表访问器
     * @return array
     */
    public function getTagnameListAttribute()
    {
        $tagNameList = [];
        $tagIdList = $this->tagid_list;
        if ($tagIdList){
            $taglist = (new \App\Services\WxUserTagService())->getTagNameListByWxTagId();
            //先交换现有标签中的键和值,然后使用键名比较计算数组的交集
            $tagNameList = array_intersect_key($taglist, array_flip($tagIdList));
            $tagNameList = array_values($tagNameList);
        }
        return $tagNameList;
    }
}